/**
 * 面试预约
 * @author  Jello
 * @create  2018/10/22 13:48
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var spinner = new Spinner(opts);
var sessionStorage = window.sessionStorage;
var userid_sign = "";
var workId = "";

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");

	//查询面试预约列表
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_workorderlist?sgin=" + userid_sign,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: 15000,
		dataType: "json",
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			spinner.spin();
			var resultObj = eval(data);
//			console.log("查询面试预约列表接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0 && resultObj.data != null) {
				if(resultObj.data.length != 0) {
					var status = "";
					var tobefull = "";
					var tbbefull_nb = "";
					var tbbefull_nbs = "";
					for(var i = 0; i < resultObj.data.length; i++) {
						var status_type = resultObj.data[i].status; //招聘状态
						if(status_type == 0) {
							status = "面试预约中";
							tobefull = "#3cafff";
							tbbefull_nbs = "none";
							tbbefull_nb = "block";
						} else if(status_type == 1) {
							status = "面试通过";
							tobefull = "#939393";
							tbbefull_nb = "none";
							tbbefull_nbs = "block";
						} else if(status_type == 2) {
							status = "面试失败";
							tobefull = "#939393";
							tbbefull_nb = "none";
							tbbefull_nbs = "block";
						} else if(status_type == 3) {
							status = "已招满";
							tobefull = "#939393";
							tbbefull_nb = "none";
							tbbefull_nbs = "block";
						} else if(status_type == 4) {
							status = "放弃入职";
							tobefull = "#939393";
							tbbefull_nb = "none";
							tbbefull_nbs = "block";
						} else if(status_type == 5) {
							status = "成功入职";
							tobefull = "#939393";
							tbbefull_nb = "none";
							tbbefull_nbs = "block";
						}
						var inter_html = '<div class="inter_box">' +
							'<div class="inter1" ' + "style=background:" + tobefull + "" + '>' +
							'<div class="inter1_left">' + resultObj.data[i].mechanismName + '</div>' +
							'<div class="inter1_right">' +
							'<span>报名时间</span>' +
							'<span>' + timestampToTime(resultObj.data[i].time) + '</span>' +
							'</div>' +
							'</div>' +
							'<div class="inter2">' +
							'<div class="inter2_left">招聘状态 :</div>' +
							'<div class="inter2_right">' + status + '</div>' +
							'</div>' +
							'<div class="inter2">' +
							'<div class="inter2_left">面试时间 :</div>' +
							'<div class="inter2_right">' + resultObj.data[i].interviewTime + '</div>' +
							'</div>' +
							'<div class="inter2">' +
							'<div class="inter2_left">应聘岗位 :</div>' +
							'<div class="inter2_right">' + resultObj.data[i].workName + '</div>' +
							'</div>' +
							'<div class="thach_list"></div>' +
							'<div class="inter2">' +
							'<div class="inter2_left">面试地点 :</div>' +
							'<div class="inter2_right">' +
							'<div class="r_f">' + resultObj.data[i].recruitAddress + '</div>' +
							//						'<div class="r_f f_bak">' +
							//						'<div class="q_dius">去这里</div><img src="images/interviewreservationlist/luxian2x.png"></div>' +
							'</div>' +
							'</div>' +
							'<div class="inter4" ' + "style=display:" + tbbefull_nbs + "" + ' onclick="deleteApply(' + resultObj.data[i].id + ')">删除</div>' +
							'<div class="inter3" ' + "style=display:" + tbbefull_nb + "" + ' onclick="cancel(' + resultObj.data[i].workId + ')">取消报名</div>' +
							'</div>';
						$(".inter_div").append(inter_html);
						if(resultObj.data[i].teacherList.length != 0) {
							var teacherList = "";
							if(resultObj.data[i].teacherList.length > 2){
								teacherList = resultObj.data[i].teacherList.slice(0,2);
							}else{
								teacherList = resultObj.data[i].teacherList;
							}
							for(var j = 0; j < teacherList.length; j++) {
								var teacherList_html = '<div class="inter2">' +
									'<div class="inter2_left">驻场老师 :</div>' +
									'<div class="inter2_right">' + teacherList[j].teacherName + '-' + teacherList[j].teacherTel + '</div>' +
									'</div>';
								$(".thach_list").eq(i).append(teacherList_html);
							}
						}
					}
				} else { //没有面试预约
					$("#notInterviewList").css("display", "block");
				}
			} else {
				$.myToast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				$.myToast("请求超时");
			} else {
				$.myToast("请求网络服务数据失败");
			}
			return;
		}
	});

	//取消
	$("#rebox_cancel").click(function() {
		$(".int_rev").css("display", "none");
		$(".int_revbox").css("display", "none");
	})

	//确定
	$("#rebox_sure").click(function() {
		//取消报名接口上送
		var param = {};
		param.userid_sign = userid_sign;
		param.workId = workId;
		cancelApply(param, function(resultObj) {
			$.myToast("取消成功");
			setTimeout(function() {
				window.location.reload();
			}, 30);
			$(".int_rev").css("display", "none");
			$(".int_revbox").css("display", "none");
		});
	})

})

/**
 * 删除报名
 * @param {Object} id 面试列表id
 */
function deleteApply(id) {
	var id = id;
	$.ajax({
		type: "get",
		url: serverUrl + "/work/del_workorder?sgin=" + userid_sign + "&workOrderId=" + id,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: 15000,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				$.myToast("删除成功");
				setTimeout(function() {
					window.location.reload();
				}, 30);
			} else {
				$.myToast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				$.myToast("请求超时");
			} else {
				$.myToast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 取消报名
 * @param {Object} id 面试预约id
 */
function cancel(id) {
	workId = id;
	$(".int_rev").css("display", "block");
	$(".int_revbox").css("display", "block");
}